/*
 * SiFiSy - A File System Simulator
 * Copyright (C) 2010  SevenSoft
 *
 * This file is part of SiFiSy.
 *
 * SiFiSy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SiFiSy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SiFiSy.  If not, see <http://www.gnu.org/licenses/>.
 */

package sifisy.newgui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import sifisy.core.Model;

/**
 * @author luca
 * @version %I%, %G%
 * @createData 30/05/2010
 */

public class WizardWidget extends JDialog {
    private JToolBar toolBar;
    private JButton btOpen;
    private JButton btNewSimulation;
    private JButton btNewProject;
    //private JScrollPane infoScrollPane;
    private JEditorPane infoTextPane;
    private JPanel southPanel;
    private JCheckBox ckDontShowAgain;
    private JButton btCloseWizard;

    public WizardWidget() {
        super(MainWidget.getMainWidgetFrame(), "Wizard", true);

        initComponents();
        setLayout(new BorderLayout());

        add(toolBar, BorderLayout.NORTH);
        add(infoTextPane, BorderLayout.CENTER);
        add(southPanel, BorderLayout.SOUTH);

        setSize(450, 300);
        setIconImage(new ImageIcon(getClass().getResource("icons/small/wizard.png")).getImage());
        setLocationRelativeTo(MainWidget.getMainWidgetFrame());
    }

    private void initComponents() {
        toolBar = new JToolBar("Wizard ToolBar");
        toolBar.setFloatable(false);
        btOpen = new JButton(Model.translate("open"));
        btOpen.setIcon(new ImageIcon(getClass().getResource("icons/small/open.png")));
        btNewSimulation = new JButton(Model.translate("newSimulation"));
        btNewSimulation.setIcon(new ImageIcon(getClass().getResource("icons/small/newSimulation.png")));
        btNewProject = new JButton(Model.translate("newProject"));
        btNewProject.setIcon(new ImageIcon(getClass().getResource("icons/small/newProject.png")));

        toolBar.add(btOpen);
        toolBar.add(btNewSimulation);
        toolBar.add(btNewProject);
        //infoScrollPane = new JScrollPane();
        infoTextPane = new JEditorPane("text/html;", "");
        infoTextPane.setBorder(new TitledBorder(Model.translate("information")));

        southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        ckDontShowAgain = new JCheckBox(Model.translate("dontShowAgain"), false);
        btCloseWizard = new JButton(Model.translate("closeButton"));
        btCloseWizard.setIcon(new ImageIcon(getClass().getResource("icons/small/close.png")));
        southPanel.add(ckDontShowAgain);
        southPanel.add(btCloseWizard);

        // adding listeners


        btOpen.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (MainWidget.open());
                    dispose();
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                infoTextPane.setText(Model.technicalHelp("wizardOpenSimulation"));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                infoTextPane.setText("");
            }
        });
        btNewSimulation.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                MainWidget.createSimulation("Default");
                dispose();
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                infoTextPane.setText(Model.technicalHelp("wizardNewSimulation"));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                infoTextPane.setText("");
            }
        });
        btNewProject.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new NewProjectWidget(null);
                dispose();
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                infoTextPane.setText(Model.technicalHelp("wizardNewProject"));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                infoTextPane.setText("");
            }
        });
        ckDontShowAgain.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                switch (e.getStateChange()) {
                    case (ItemEvent.SELECTED): {
                        Model.setShowWizard("false");
                        break;
                    }
                    case (ItemEvent.DESELECTED): {
                        Model.setShowWizard("true");
                        break;
                    }
                }
                Model.saveSetup();
            }
        });
        btCloseWizard.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                dispose();
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                infoTextPane.setText(Model.technicalHelp("wizardCloseWizard"));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                infoTextPane.setText("");
            }
        });
    }
}
